Skip to content

SubsetFilterProxyModel

Base classes

Name Children Inherits
SortFilterProxyModel
prettyqt.core.sortfilterproxymodel
Support for sorting and filtering data passed between another model and a view.

⋔ Inheritance diagram

graph TD
  33498611["itemmodels.SubsetFilterProxyModel"]
  2548039224["core.SortFilterProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  153997648["QtCore.QSortFilterProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2548039224 --> 33498611
  2431222121 --> 2548039224
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  153997648 --> 2548039224
  3989152870 --> 153997648
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SortFilterProxyModel

A FilterProxyModel to filter based on slices, ranges, indexes or Callables.

Example

proxy = itemmodels.SubsetFilterProxyModel()
proxy.set_source_model(model)
table.set_model(proxy)
table.show()

!!! note If you only need filtering based on slices or a single column / row, the SliceFilterProxymodel should be preferred for performance reasons.

Source code in prettyqt/itemmodels/proxies/subsetfilterproxymodel.py
class SubsetFilterProxyModel(core.SortFilterProxyModel):
    """A FilterProxyModel to filter based on slices, ranges, indexes or Callables.

    ### Example

    ```py
    proxy = itemmodels.SubsetFilterProxyModel()
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```

    !!! note
        If you only need filtering based on slices or a single column / row,
        the SliceFilterProxymodel should be preferred for performance reasons.
    """

    ID = "subset"

    def __init__(
        self,
        row_filter: slice | range | int | Container[int] | Callable | None,
        column_filter: slice | range | int | Container[int] | Callable | None,
        **kwargs,
    ):
        self.row_filter = row_filter
        self.column_filter = column_filter
        super().__init__(**kwargs)

    def __repr__(self):
        return get_repr(self, self.row_filter, self.column_filter)

    def filterAcceptsColumn(self, source_column: int, parent: core.ModelIndex) -> bool:
        match self.column_filter:
            case slice() | range():
                return helpers.is_in_slice(self.column_filter, source_column)
            case int():
                return source_column == self.column_filter
            case Container():
                return source_column in self.column_filter
            case Callable():
                return self.column_filter(source_column)
            case None:
                return True
            case _:
                raise ValueError(self.column_filter)

    def filterAcceptsRow(self, source_row: int, parent: core.ModelIndex) -> bool:
        match self.row_filter:
            case slice() | range():
                return helpers.is_in_slice(self.row_filter, source_row)
            case int():
                return source_row == self.row_filter
            case Container():
                return source_row in self.row_filter
            case Callable():
                return self.row_filter(source_row)
            case None:
                return True
            case _:
                raise ValueError(self.row_filter)

:fa-brands-github: Show source on GitHub